package com.daiqee.aspect;

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.daiqee.enums.RequestType;
import com.daiqee.vo.RestFulResultVO;

/**
 * 统一的Restful异常处理,并返回响应的状态码
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.4.1
 * @author RichardTang 
 * @date: 2018年6月23日
 * @package com.daiqee.aspect
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@SuppressWarnings("rawtypes")
public class HttpStatusAspect {

	/**
	 * 调用方法前执行
	 */
    public void before(JoinPoint joinPoint){
    }  
  
    /**
     * 调用方法后执行-无论执行成功或者失败都会执行
     */
    public void after(JoinPoint joinPoint){
    }  
  
    /**
     * 方法执行后带参数返回-执行成功时执行
     */
    public void afterReturning(JoinPoint joinPoint, Object returnValue){
    }  
  
    /**
     * 抛出异常时执行
     */
    public void afterThrowing(JoinPoint joinPoint, Throwable e){
    }  
    
    /**
     * 环绕
     */
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    	try {
    		Object restFulResultVO = proceedingJoinPoint.proceed();
        	
        	if(restFulResultVO instanceof RestFulResultVO) {

        		RestFulResultVO vo = (RestFulResultVO) restFulResultVO;
        		
        		Object result = vo.getResult();
        		RequestType type = vo.getType();
        		
        		if(isNotBlank(result)) {
        			//当传递过来的参数为空的时候,判断他的请求类型
        			return successStatusCode(type,result);
        		}else {
        			return failStatusCode(type);
        		}
        	}else {
        		return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        	}
        } catch (Exception e) {
        	return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }
    
    /**
     * <p>判断集合或者类对象是否为空</p>
     * <P>(Collection = null) = false</p>
     * <P>(Collection < 0) = false</p>
     * <p>(Obj = null) = false</p>
     * 不为空: true
     * 为空: false
     */
	public boolean isNotBlank(Object obj) {
    	if(obj != null) {
    		if(obj instanceof Collection && ((Collection) obj).isEmpty()) {
        		return false;
        	}else if(obj instanceof String && !(StringUtils.isNotBlank((String) obj))) {
        		return false;
        	}else if(obj instanceof Boolean) {
        		return (boolean) obj;
        	}
    		return true;
    	}
    	return false;
    }
    
    /**
     * 请求失败时的状态码<br/>
     * GET:请求返回404状态码,其余请求都为400
     */
    public ResponseEntity<Object> failStatusCode(RequestType type){
    	if(RequestType.GET.equals(type)) {
    		return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
    	}
		return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
    } 
    
    /**
     * 请求成功时的状态码
     */
    public ResponseEntity<Object> successStatusCode(RequestType type,Object entity){
		if(RequestType.POST.equals(type)) {
			return ResponseEntity.status(HttpStatus.CREATED).body(entity);
		}
    	return ResponseEntity.status(HttpStatus.OK).body(entity);
    } 
}
